<?php 
	jimport('joomla.application.component.model');
	class kinh_doanhModelxu_ly_don_hang extends JModel
	{
        function lay_id_sp()
        {
            //echo "ss";die;
            $user = JFactory::getUser();
                    //dang nhap thanh cong lay duoc id -> lay id_a_b tu table bran_user, 
                    $sql = "select id_a_b from #__erp_agent_branch_user where id_user ='".$user->get('id')."' ";
                    $db = &JFactory::getDBO();
        		    $db->setQuery($sql);
        		    $id_a_b = $db->loadResult();
                    $_SESSION['id_a_b'] = $id_a_b; 	
            $sql = "select ep.id_product
                            from #__erp_product ep ,#__erp_product_type ept ,#__erp_agent_branch eab,#__erp_agent_branch_warehouse eabw ,  #__erp_financial_currency cur, #__erp_product_unit ui
                            where 
                            ep.id_type_product = ept.id and 
                            ep.id_product = eabw.id_product and
                            eab.id = eabw.id_a_b and
                            ep.id_currency = cur.id and
                            ui.id = ep.id_unit and
                            eab.id = '".$id_a_b."' 
                            
                        ";
                        
            print_r($db->setQuery($sql));
			return $db->loadObjectList();
        }
        function cap_nhat_balance_khach($id_cus,$tt)
        {
            $db=&JFactory::getDBO();
            $sql = "select balance from #__erp_product_custom_account where id_cus = '".$id_cus."'";
                                $db->setQuery($sql);
                                $balance = $db->loadResult();
                                $balance = $balance - $tt;
            $sql = "UPDATE #__erp_product_custom_account SET 
                                balance = '".$balance."'
                                WHERE 
                                id_cus = '".$id_cus."'";
                                $db->setQuery($sql);
                                $db->query(); 
        }	   
		function xu_ly($id_product,$cp,$id_cus,$sl,$pt,$tt,$id_cur,$date)  
                        
                        {   
                            // kiem tra lai id_a_b
                            $user = JFactory::getUser();
                            global $mainframe;
                            $db=&JFactory::getDBO();
                            $sql = "select id_a_b from #__erp_agent_branch_user where id_user ='".$user->get('id')."' ";
                            $db->setQuery($sql);
                            $id_a_b = $db->loadResult();
                            //echo $id_a_b;
                            
                                    
                                    $row =  & JTable::getInstance('kinh_doanh', 'Table'); // goi file tuong ung trong table
                                    $row->id_a_b = $id_a_b; //do cac name cua form khac voi cau truc file table nen phai gan tung gia tri
                                    $row->id_product = $id_product;
                                    $row->id_cuppon = $cp;
                                	$row->id_customer = $id_cus;
                                	$row->date = $date;
                                    $row->number_of = $sl;
                                	$row->cus_paid = $pt;
                                	$row->total = $tt;
                                	$row->id_currency = $id_cur;
                                    
                                    //neu cac name cua file trung voi cau truc file thi dung 
                                    //if (!$row->bind( $post )) { JError::raiseError(500, $row->getError() ); }  de truyen cac gia tri vao
                                    
                              	    if (!$row->store()) { //thuc thi them record  neu co loi se hien thi
                              	     JError::raiseError(500, $row->getError() );
                                        }
                                    // muon lay id cua record moi tao dung $row->id 
                                    //hoi den file jtable can insert
                                    //$row =  & JTable::getInstance('check', 'Table'); // goi gile table
                                    //gan gia tri
                                    //$row->thuoctinh = bien post lay tu form
                                    //neu ten form giong ten bien thi dung if (!$row->bind( $post )) { JError::raiseError(500, $row->getError() ); } 
                                    //if (!$row->store()) { JError::raiseError(500, $row->getError() ); }  //thuc thi insert
                                    //echo $row->store; show ra id vua them
                                    
                                    //them record
                                    //$sql = "INSERT INTO #__erp_agent_branch_sale (id_a_b,id_product,id_cuppon,id_customer,date,
                                                                                //number_of,cus_paid,total,id_currency,note,orther)
                                                                                //VALUES ('".$id_a_b."','".$id_product."','".$cp."','".$id_cus."','".$date."','".$sl."','".$pt."',
                                                                                //'".$tt."','".$id_cur."','".$gh."',0)";
                                  //$db->setQuery($sql);
                                  //$rs = $db->query();  
                                   
                                  //cuppon
                                $sql = "UPDATE #__erp_cuppon SET
                                used = 1
                                WHERE 
                                id ='".$cp."'";
                                $db->setQuery($sql);
                                $db->query();
                                /*if($db->errorNum !='')
                                {
                                    //xoa record vua tao
                                    $row->delete($row->id);
                                    $mainframe->redirect( "index.php?option=com_kinh_doanh", "Bạn nhập sai thông tin hoặc chưa đủ","Error");
                                }*/
                                //neu co loi o day thi thang tren khong chay $db->errorNum !='' thi co loi, dung $row->delete($row->store);
                                
                               
                                
                                  //lay so luong trong kho
                                $sql = "select number_of from #__erp_agent_branch_warehouse where id_a_b = '".$id_a_b."' and id_product = '".$id_product."'";
                                $db->setQuery($sql);
                                $num = $db->loadResult();
                                $num = $num - $sl; 
                                $sql = "UPDATE #__erp_agent_branch_warehouse SET 
                                number_of = '".$num."'
                                WHERE 
                                id_a_b = '".$id_a_b."' and id_product = '".$id_product."'";
                                $db->setQuery($sql);
                                $db->query();
                                /*if($db->errorNum !='')
                                {
                                    //xoa record vua tao
                                    $row->delete($row->id);
                                    
                                    //cap nhat lai cuppon
                                    $sql = "UPDATE #__erp_cuppon SET
                                    used = 0
                                    WHERE 
                                    id ='".$cp."'";
                                    $db->setQuery($sql);
                                    $db->query();
                                    $mainframe->redirect( "index.php?option=com_kinh_doanh", "Bạn nhập sai thông tin hoặc chưa đủ","Error");
                                }
                                
                                */     
                                $sql = "UPDATE #__erp_agent_branch_account SET 
                                balance = balance + '".$tt."'
                                WHERE 
                                id_a_b ='".$id_a_b."'";
                                $db->setQuery($sql);
                                $db->query();
                                
                                
                                //cap nhat lai so tien trong tai khoan neu khach thang toan bang the
                                
                                 #__erp_product_custom_account
                                /*if($db->errorNum !='')
                                {
                                    //xoa record vua tao
                                    $row->delete($row->id);
                                    
                                    //cap nhat lai cuppon
                                    $sql = "UPDATE #__erp_cuppon SET
                                    used = 0
                                    WHERE 
                                    id ='".$cp."'";
                                    $db->setQuery($sql);
                                    $db->query();
                                    
                                    //cap nha lai so luong trong kho
                                    $sql = "select number_of from #__erp_agent_branch_warehouse where id_a_b = '".$id_a_b."' and id_product = '".$id_product."'";
                                    $db->setQuery($sql);
                                    $num = $db->loadResult();
                                    $num = $num + $sl; 
                                    $sql = "UPDATE #__erp_agent_branch_warehouse SET 
                                    number_of = '".$num."'
                                    WHERE 
                                    id_a_b = '".$id_a_b."' and id_product = '".$id_product."'";
                                    $db->setQuery($sql);
                                    $db->query();
                                    
                                    $mainframe->redirect( "index.php?option=com_kinh_doanh", "Bạn nhập sai thông tin hoặc chưa đủ","Error");
                                }*/

                                return;
                
		}	
	}
?>